// 示例使用

#include "../logsystem/snowdragon.h"

// 局部日志器建造者，同步日志器
void SynchronousTest(const std::string& logger_name)
{
    snowdragon::LocalLoggerBuilder::ptr llb(new snowdragon::LocalLoggerBuilder);         // 日志器建造者
    llb->buildLoggerName(logger_name.c_str());
    llb->buildLoggerType(snowdragon::Logger::LoggerType::SYNC_LOGGER);
    llb->buildLoggerLevel(snowdragon::LogLevel::Level::DEBUG);
    llb->buildLoggerSink<snowdragon::specificFileSink>("./logfiles/local_sync_test.log");
    llb->buildLoggerSink<snowdragon::StdOutSink>();
    snowdragon::Logger::ptr logger = llb->build();

    DEBUG_LOG_DEFAULT("测试开始");

    logger->debug("测试logger.hpp相关操作, %s", "writed by snowdragon");
    logger->info("测试logger.hpp相关操作, %s", "writed by snowdragon");
    logger->warn("测试logger.hpp相关操作, %s", "writed by snowdragon");
    logger->error("测试logger.hpp相关操作, %s", "writed by snowdragon");
    logger->fatal("测试logger.hpp相关操作, %s", "writed by snowdragon");

    std::cout << "\n\n";
    DEBUG_LOG(logger, "测试logger.hpp相关操作, %s", "writed by snowdragon");
    INFO_LOG(logger, "测试logger.hpp相关操作, %s", "writed by snowdragon");
    WARN_LOG(logger, "测试logger.hpp相关操作, %s", "writed by snowdragon");
    ERROR_LOG(logger, "测试logger.hpp相关操作, %s", "writed by snowdragon");
    FATAL_LOG(logger, "测试logger.hpp相关操作, %s", "writed by snowdragon");

    DEBUG_LOG_DEFAULT("测试结束");

    for (int i = 0; i < 300; ++i)
    {
        logger->fatal("测试logger.hpp相关操作 %d", i);
    }
}

// 全局日志器建造者，异步日志器
void AsynchronousTest(const std::string& logger_name)
{
    snowdragon::GlobalLoggerBuilder::ptr glb(new snowdragon::GlobalLoggerBuilder);         // 日志器建造者
    glb->buildLoggerName(logger_name.c_str());
    glb->buildLoggerType(snowdragon::Logger::LoggerType::ASYNC_LOGGER);
    glb->buildLoggerLevel(snowdragon::LogLevel::Level::DEBUG);
    glb->buildLoggerSink<snowdragon::specificFileSink>("./logfiles/global_async_test.log");
    glb->buildLoggerSink<snowdragon::StdOutSink>();
    snowdragon::Logger::ptr logger = glb->build();
    // 修改异步日志器的安全类型函数的使用，默认是安全的
    // logger->modifyAsyncLoggerAndLooperSafeType(snowdragon::AsyncLooperSafeType::UNSAFE_ASYNC_LOOPER);
   
    // snowdragon::LoggerManager::getInstance().pushLogger(logger);     // 全局日志器建造者在调用build函数后，就会自动将其加入管理者中，不需要手动弄

    // 以下是对日志器的弹出和压入测试，一般使用不到
    snowdragon::LoggerManager::getInstance().popLogger(logger_name);
    snowdragon::LoggerManager::getInstance().pushLogger(logger);

    DEBUG_LOG_DEFAULT("测试开始");

    logger->debug("测试logger.hpp相关操作, %s", "writed by snowdragon");            // 上面创建的日志器调用
    logger->info("测试logger.hpp相关操作, %s", "writed by snowdragon");
    logger->warn("测试logger.hpp相关操作, %s", "writed by snowdragon");
    logger->error("测试logger.hpp相关操作, %s", "writed by snowdragon");
    logger->fatal("测试logger.hpp相关操作, %s", "writed by snowdragon");

    std::cout << "\n\n";
    DEBUG_LOG_DEFAULT("测试logger.hpp相关操作, %s", "writed by snowdragon");        // 默认的日志器调用，即全局名为root的日志器
    INFO_LOG_DEFAULT("测试logger.hpp相关操作, %s", "writed by snowdragon");
    WARN_LOG_DEFAULT("测试logger.hpp相关操作, %s", "writed by snowdragon");
    ERROR_LOG_DEFAULT("测试logger.hpp相关操作, %s", "writed by snowdragon");
    FATAL_LOG_DEFAULT("测试logger.hpp相关操作, %s", "writed by snowdragon");

    std::cout << "\n\n";
    DEBUG_LOG(logger, "测试logger.hpp相关操作, %s", "writed by snowdragon");
    INFO_LOG(logger, "测试logger.hpp相关操作, %s", "writed by snowdragon");
    WARN_LOG(logger, "测试logger.hpp相关操作, %s", "writed by snowdragon");
    ERROR_LOG(logger, "测试logger.hpp相关操作, %s", "writed by snowdragon");
    FATAL_LOG(logger, "测试logger.hpp相关操作, %s", "writed by snowdragon");

    DEBUG_LOG_DEFAULT("测试结束");

    for (int i = 0; i < 30000; ++i)
    {
        logger->fatal("测试logger.hpp相关操作 %d", i);
    }
}

int main()
{
    // SynchronousTest("synchronous");
    AsynchronousTest("asynchronous");

    return 0;
}